package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @Author: ycy
 * @CreateDate: 2019/3/6 10:08
 */
public class IoDemo2 {


  public static void main(String[] args) {
    System.out.println("テ".length());
  }

  private static List<String> tenTagList =
          Lists.newLinkedList(Arrays.asList("温泉", "滑雪", "踏青","海岛"));


  private static List<String> fiveTagList =
          Lists.newLinkedList(Arrays.asList("岛屿"));

  private static List<String> threeTagList =
          Lists.newLinkedList(Arrays.asList("沙漠","冲浪"));


  private static List<String> twoTagList =
          Lists.newLinkedList(Arrays.asList(""));


  private static String sepator = ",";


//  public static void main(String[] args) {
//    IoDemo2 ioDemo2 = new IoDemo2();
//
//    //获取districtid districtname 对应关系
//    Map<Integer, String> districtName = ioDemo2.getDistrictName();
//
//    Map<Integer, String> districtTagname = ioDemo2.getDistrictTagname();
//
//    //获取words
//    Map<String, String> themeTagname = ioDemo2.getThemeTagname();
//    Set<String> theme = Sets.newHashSet();
//    for (Map.Entry<String, String> entry : themeTagname.entrySet()) {
//      theme.add("," + entry.getValue() + ",");
//    }
//
//    int max = 0;
//    int count = 0;
//    for (Map.Entry<Integer, String> integerStringEntry : districtTagname.entrySet()) {
//      String value = integerStringEntry.getValue();
//      Integer key = integerStringEntry.getKey();
//      System.out.println(districtName.get(key) + "=" + value);
//      if (value.length() > max) {
//        max = value.length();
//      }
//      Iterator<String> themeit = theme.iterator();
//      while (themeit.hasNext()) {
//        String next = themeit.next();
//        if (StringUtils.contains("," + value + ",", next)) {
//          themeit.remove();
//        }
//      }
//      count++;
//    }
//    System.out.println(max);
//    System.out.println(count);
//    System.out.println(Arrays.toString(theme.toArray()));
//  }

  public Map<Integer, String> getDistrictTagname() {
    Map<Integer, String> districtTagMaps = Maps.newHashMap();
    // {districtid:{博物馆:3,动物园:2}}
    Map<Integer, Map<String, Integer>> districtTagMapsAid = Maps.newHashMap();
    Map<String, String> themeTagname = getThemeTagname();

    //从gl_poi 获取
    List<GlPoi> glPoiList = getGlpoi();

    //从gl_producttaginfo 获取
    Map<Long, String> glProductTagInfo = getGlProductTagInfo();

    try {
      //整合
      for (GlPoi glPoi : glPoiList) {
        String tag = glProductTagInfo.get(glPoi.businessid);
        if (StringUtils.isNotBlank(tag)) {
          glPoi.tagsname += sepator + tag;
        }

        String[] districtidArr = glPoi.districtpathid.split(",");
        for (String districtidStr : districtidArr) {
          Integer districtid = Integer.parseInt(districtidStr.trim());
          Map<String, Integer> tagCountMap = districtTagMapsAid.get(districtid);

          //转化词典里面的word
          List<String> themeSet = Lists.newLinkedList();
          for (String tagName : glPoi.tagsname.split(",")) {
            String themeword = themeTagname.get(tagName.trim());
            if (StringUtils.isNotBlank(themeword)) {
              themeSet.add(themeword);
            }
          }
          String themeJoin = StringUtils.join(themeSet.iterator(), ",");
//          String themeJoin = glPoi.tagsname;
          if (tagCountMap != null) {
            //已经有了
            countTag(tagCountMap, themeJoin);
          } else {
            //没有
            tagCountMap = Maps.newHashMap();
            countTag(tagCountMap, themeJoin);
            districtTagMapsAid.put(districtid, tagCountMap);
          }
        }

      }

    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {

    }

    //合并
    //取tagName符合出现次数的
    for (Map.Entry<Integer, Map<String, Integer>> districtTagMap : districtTagMapsAid.entrySet()) {
      Integer districtId = districtTagMap.getKey();

      Map<String, Integer> districtTagCountMap = districtTagMap.getValue();
      Set<String> tagNameSet = Sets.newTreeSet();
      for (Map.Entry<String, Integer> tagCountMap : districtTagCountMap.entrySet()) {
        String tagName = tagCountMap.getKey();
        Integer count = tagCountMap.getValue();
        if (tagName != null
                && (count > 10 || !tenTagList.contains(tagName))
                && (count > 5 || !fiveTagList.contains(tagName))
                && (count > 3 || !threeTagList.contains(tagName))
                && (count > 2 || !twoTagList.contains(tagName))
                ) {
          tagNameSet.add(tagName);
        }
      }
      if (!tagNameSet.isEmpty()) {
        districtTagMaps.put(districtId, StringUtils.join(tagNameSet.iterator(), sepator));
      }
    }
    return districtTagMaps;
  }


  private void countTag(Map<String, Integer> newTagCountMap, String tagNameJoin) {
    //已经有了
    for (String tagNameElement : tagNameJoin.split(sepator)) {
      String tagNameElementTrim = tagNameElement.trim();
      if (StringUtils.isBlank(tagNameElementTrim)) {
        continue;
      }
      Integer count = newTagCountMap.get(tagNameElementTrim);
      if (newTagCountMap.containsKey(tagNameElementTrim)) {
        newTagCountMap.put(tagNameElementTrim, count + 1);
      } else {
        newTagCountMap.put(tagNameElementTrim, 1);
      }
    }
  }

  /**
   * select businessid,districtpathid,tagsname,themesname from gl_poi where poitype='sight' and isPublish = 1 and districtpathid is not null;
   */
  private List<GlPoi> getGlpoi() {
    List<GlPoi> poi = Lists.newLinkedList();
    //从gl_producttaginfo 获取标签
    File file = new File("D:\\Users\\yincy\\Desktop\\标签\\poi.txt");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {
        String alias = br.readLine().trim();
        String[] split = alias.split("=", -1);
        String businessid = split[0].trim();
        String districtpathid = split[1].trim();
        String tagsname = split[2].trim();
        String themesanem = split[3].trim();
        if (StringUtils.isNotBlank(districtpathid) && (StringUtils.isNotBlank(tagsname) || StringUtils.isNotBlank(themesanem))) {
          poi.add(new GlPoi(Long.parseLong(businessid), districtpathid, tagsname + sepator + themesanem));
        }

      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
    }
    return poi;
  }

  class GlPoi {
    Long businessid;
    String districtpathid;
    String tagsname;

    public GlPoi(Long businessid, String districtpathid, String tagsname) {
      this.businessid = businessid;
      this.districtpathid = districtpathid;
      this.tagsname = tagsname;
    }
  }

  /**
   * SELECT info.productid,info.tag  FROM gl_producttaginfo info  where info.isdelete='F' and info.producttype = 'sight';
   */
  private Map<Long, String> getGlProductTagInfo() {
    Map<Long, String> districtMaps = Maps.newHashMap();
    //从gl_producttaginfo 获取标签
    File file = new File("D:\\Users\\yincy\\Desktop\\标签\\producttaginfo.txt");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {
        String alias = br.readLine().trim();
        String[] split = alias.split("=", -1);
        String districtpathid = split[0].trim();
        String tag = split[1].trim();

        long key = Long.parseLong(districtpathid);
        if (districtMaps.containsKey(key)) {
          String tags = districtMaps.get(key);
          districtMaps.put(key, tags + sepator + tag);
        } else {
          districtMaps.put(key, tag);
        }

      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
    }
    return districtMaps;
  }


  /**
   * 通过districtid 获取districtname
   *
   * @return
   */
  public Map<Integer, String> getDistrictName() {
    Map<Integer, String> districtMaps = Maps.newHashMap();

    //从gl_poi获取数据
    File file = new File("D:\\Users\\yincy\\Desktop\\标签\\district.txt");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {
        String alias = br.readLine().trim();
        String[] split = alias.split("=", -1);
        String districtpathid = split[0].trim();
        String tagsname = split[1].trim();

        districtMaps.put(Integer.parseInt(districtpathid), tagsname);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
    }
    return districtMaps;
  }

  private Map<String, String> getThemeTagname() {
    Map<String, String> themeMaps = Maps.newHashMap();

    //从gl_poi获取数据
    File file = new File("D:\\Users\\yincy\\Desktop\\标签\\word.txt");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {

       /* String alias = br.readLine().trim();
        String[] split = alias.split("\\|", -1);
        for (String theme : split) {
          String trim = theme.trim();
          if (StringUtils.isNotBlank(trim)) {
            themeMaps.put(trim, trim);
          }
        }*/
        String readLine = br.readLine();
        String[] splits = readLine.split("=");
        String word = splits[0];
        String themes = splits[1];
        String[] split = themes.split("\\|", -1);
        for (String theme : split) {
          String trim = theme.trim();
          if (StringUtils.isNotBlank(trim)) {
            themeMaps.put(trim, word.trim());
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
    }

    return themeMaps;
  }


}
